using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;

namespace SharePoint.Orm.Wrappers {
	/// <summary>
	/// A <see cref="SPGroup"/> backed version of the <see cref="ISPOrmGroup"/>
	/// </summary>
	public class SPOrmSharePointGroup : ISPOrmGroup {
		private readonly SPGroup _group;

		/// <summary>
		/// constructor.
		/// </summary>
		/// <param name="group">The <see cref="SPGroup"/>.</param>
		public SPOrmSharePointGroup(SPGroup group) {
			_group = group;
		}

		/// <summary>
		/// Gets the <see cref="SPGroup"/>.
		/// </summary>
		/// <returns>The <see cref="SPGroup"/></returns>
		public SPPrincipal GetSPPrincipal() {
			return _group;
		}

		///<summary>
		/// Gets the users in the group.
		///</summary>
		///<returns>The users</returns>
		public virtual IEnumerable<ISPOrmUser> GetUsers() {
			return _group.Users.Cast<SPUser>().Select(u => (ISPOrmUser)new SPOrmSharePointUser(u));
		}

		public virtual void AddUser(ISPOrmUser user) {
			_group.AddUser((SPUser)user.GetSPPrincipal());
		}

		public virtual void RemoveUser(ISPOrmUser user) {
			_group.RemoveUser((SPUser)user.GetSPPrincipal());
		}

		/// <summary>
		/// The id of the group.
		/// </summary>
		public int Id {
			get { return _group.ID; }
		}

		/// <summary>
		/// The display name of the group.
		/// </summary>
		public string Name {
			get { return _group.Name; }
		}
	}
}